package com.grp6.game.ui.platform.text.ansi;

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;

import com.grp6.game.ui.platform.ICell;

public class UICellMapper {
	private HashMap<String,Character> symbol2char = new HashMap<String, Character>();
	private HashMap<String,Color> symbol2fg = new HashMap<String, Color>();
	private HashMap<String,Boolean> symbol2light = new HashMap<String, Boolean>();
	private HashMap<String,Color> tile2bg = new HashMap<String, Color>();
	
	private static final String FGLIGHT_SUFFIX = ".fgLight";
	private static final String FG_SUFFIX = ".fgColor";
	private static final String BG_SUFFIX = ".bgColor";
	private static final String CHAR_SUFFIX = ".char";
	
	public UICellMapper(){
		Properties prps = new Properties();
		try {
			prps.load(ICell.class.getClassLoader().getResourceAsStream("uimap.properties"));
			for(Entry<Object, Object> e: prps.entrySet()){
				String key = (String)e.getKey();
				String value = (String)e.getValue();
				if (key.endsWith(FG_SUFFIX))
					symbol2fg.put(key.substring(0,key.length()-FG_SUFFIX.length()), Color.valueOf(value));
				else if (key.endsWith(BG_SUFFIX))
					tile2bg.put(key.substring(0,key.length()-BG_SUFFIX.length()), Color.valueOf(value));
				else if (key.endsWith(CHAR_SUFFIX))
					symbol2char.put(key.substring(0,key.length()-CHAR_SUFFIX.length()), value.charAt(0));
				if (key.endsWith(FGLIGHT_SUFFIX))
					symbol2light.put(key.substring(0,key.length()-FGLIGHT_SUFFIX.length()), Boolean.parseBoolean(value));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public char toChar(String symbol) {
		Character ch = symbol2char.get(symbol);
		if (ch!=null)
			return ch;
		else
			return ' ';
	}

	public Color toFGColor(String symbol) {
		Color fg = symbol2fg.get(symbol);
		if (fg!=null)
			return fg;
		else
			return Color.Black;
	}
	
	public Boolean toFGLight(String symbol) {
		Boolean light = symbol2light.get(symbol);
		if (light!=null)
			return light;
		else
			return false;
	}

	public Color toBGColor(String tile) {
		Color bg = tile2bg.get(tile);
		if (bg!=null)
			return bg;
		else
			return Color.LightGray;
	}
}
